package com.blackhorse.controller;

import com.alibaba.fastjson.JSON;
import com.blackhorse.domain.Product;
import com.blackhorse.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 商品信息查询接口
     */
    @RequestMapping("/product/{pid}")
    public Product product(@PathVariable("pid") Integer pid) {
        log.info("商品微服务-查询商品信息-开始 pid:{}", pid);
        Product product = productService.findByPid(pid);
        log.info("商品微服务-查询商品信息-成功 product:{}", JSON.toJSONString(product));
        return product;
    }


    /**
     * 扣减商品库存接口
     */
    @RequestMapping("/product/reduceInventory")
    public String reduceInventory(@RequestParam("pid") Integer pid,
                                   @RequestParam("number") Integer number) {
        log.info("商品微服务-扣减商品库存-开始 pid:{} number:{}", pid, number);
        productService.reduceInventory(pid, number);
        log.info("商品微服务-扣减商品库存-成功");
        return "success";
    }
}
